package Parsers;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import BusinessObjects.User;
import Main.MainProgram;
public class UserParser extends DefaultHandler {
private MainProgram program;
private Collection<User> users;
private Collection<String> usersToDelete;
private User currentUser;
private String currentBalise = "";
private String baliseMere = "";
public UserParser(MainProgram m) {
users = new ArrayList<User>();
usersToDelete = new ArrayList<String>();
program = m;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.compareTo("utilisateur") == 0) {
currentUser = new User();
}
if (localName.equals("ID") && baliseMere.equals("delete")){
this.usersToDelete.add(attributes.getValue(0));
}
if (localName.equals("identite") || localName.equals("coordonnee")
|| localName.equals("delete") || localName.equals("listeAvis"))
baliseMere = localName;
currentBalise = localName;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.compareTo("utilisateur") == 0) {
users.add(currentUser);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String value = new String(ch, start, length);
// On supprime les espaces inutiles
value = value.replaceAll("[\t\n]+", "");
if (value.equals("") || value.equals(" "))
return;
// System.out.println(currentBalise+ " : "+value);
if (baliseMere.equals("identite")) {
if (currentBalise.equals("nom"))
currentUser.setLastName(value);
if (currentBalise.equals("prenom"))
currentUser.setFirstName(value);
if (currentBalise.equals("age"))
currentUser.setAge(Integer.parseInt(value));
if (currentBalise.equals("pseudo"))
currentUser.setPseudo(value);
}
if (baliseMere.equals("coordonnee")) {
if (currentBalise.equals("numero"))
currentUser.setNumRue(Integer.parseInt(value));
if (currentBalise.equals("rue"))
currentUser.setRue(value);
if (currentBalise.equals("ville"))
currentUser.setVille(value);
if (currentBalise.equals("codePostal"))
currentUser.setCodePostal(value);
if (currentBalise.equals("mail"))
currentUser.setMail(value);
if (currentBalise.equals("telephone"))
currentUser.setTelephone(value);
}
if (baliseMere.equals("listeAvis") && currentBalise.equals("avis"))
currentUser.getAvis().add(value);
if (currentBalise.equals("urlPhoto"))
currentUser.setUrlPhoto(value);
}
@Override
public void endDocument() throws SAXException {
program.setUsersToCreate(users);
program.setUsersToDelete(usersToDelete);
System.out.println("Fin du fichier ; "+MainProgram.userOperations);
}
}